
#include "det_SlidingBox.h"

// [*] CImageWindowContext::CSlidingWindowList

CImageWindowContext::CSlidingWindowList::CSlidingWindowList()
{
	Clear();
}

void CImageWindowContext::CSlidingWindowList::Clear()
{
	Size = 0;
	ippList.Clear();
}

void CImageWindowContext::CSlidingWindowList::PushBack(const CImagePyramidPosition& ipp)
{
	++Size;
	ippList.PushBack(ipp);
}

void CImageWindowContext::CSlidingWindowList::BuildWindowList(double W, double H, const CSlidingConfig& SlidingConfig, bool fClearFirst)
{
	if (fClearFirst) Clear();

	double min_sf = 1.01;
	int i_scale;
	double bw, bh, tw, th, tx, ty, sx, sy, sf, dx, dy, dx2, dy2, ix, iy;
	CImagePyramidPosition t_ipp;

	bw = SlidingConfig.BaseW; bh = SlidingConfig.BaseH;
	sx = SlidingConfig.xStride; if (sx < 1.0) sx = 1.0;
	sy = SlidingConfig.yStride; if (sy < 1.0) sy = 1.0;
	sf = SlidingConfig.ScaleFactor; if (sf < min_sf) sf = min_sf;
	sf = 1.0 / sf;

	tw = W; th = H;
	tx = 1.0; ty = 1.0;
	for ( i_scale = 0; ; ++i_scale )
	{
		dx = tw - bw; dx = (dx - floor(dx / sx) * sx) / 2.0 + bw / 2.0; dx2 = tw - dx + EPS;
		dy = th - bh; dy = (dy - floor(dy / sy) * sy) / 2.0 + bh / 2.0; dy2 = th - dy + EPS;

		if (!(dx<dx2 && dy<dy2)) break;

		for ( iy = dy; iy < dy2; iy += sy ) for ( ix = dx; ix < dx2; ix += sx )
		{
			t_ipp.InitOld(ix, iy, tx, ty);
			PushBack(t_ipp);
		}
		tw *= sf; th *= sf; tx *= sf; ty *= sf;
	}
}

void CImageWindowContext::CSlidingWindowList::GenerateRandomWindow(CImagePyramidPosition& dst_ipp)
{
	if (Size < 1) return;
	int i = CMyRandom::NextInt(Size);
	dst_ipp = ippList.pElement[i];
}

